package com.eatme.core.search;

import javax.swing.table.TableRowSorter;

/**
 * 二分查找-递归
 */
public class BinarySearchV1 {
    private BinarySearchV1() {

    }

    public static <E extends Comparable<E>> int search(E[] data,E target) {
        return search(data,0,data.length-1, target);
    }

    private static <E extends  Comparable<E>> int search(E[] data,int l,int r,E target) {
        if (l>r) return -1;
        int mid = l + (r-l) /2 ; // 避免整型溢出
        if (data[mid].compareTo(target)==0)
            return mid;
        if (data[mid].compareTo(target)<0)
            return search(data,mid+1,r,target);
        return search(data,l,mid-1,target);
    }
}
